<template>
  <div class="container">
    <el-card class="box-card" shadow="always">
      <el-row :gutter="24">
        <el-col :span="16">
          <el-form inline @submit.native.prevent="search">
            <el-form-item label="搜索">
              <el-input
                v-model="searchForm.queryConditions"
                clearable
                placeholder=""
                prefix-icon="el-icon-search"
                size="medium"
              />
            </el-form-item>
            <!-- <el-form-item label="组织">
              <el-select v-model="organizationList.organizeName" clearable placeholder="全部" style="width:150px;" @change="organizationSelected">
                <el-option v-for="item in organizationList" :key="item.id" :value="item.id" :label="item.organizeName" />
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-button native-type="submit" size="medium" type="primary"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="8">
          <el-button icon="el-icon-download" type="primary" class="btn">
            <a download :href="exportUrl"> 导出 </a>
          </el-button>
        </el-col>
      </el-row>
      <div>
        <el-table :data="list" border style="width: 100%" v-loading="unreviewedSupplierLoading">
          <el-table-column
            type="index"
            width="50"
            label="序号"
            align="center"
          />
          <el-table-column label="头像" width="70" align="center">
            <template slot-scope="scope">
              <el-avatar :src="scope.row.avatar"></el-avatar>
            </template>
          </el-table-column>

          <el-table-column prop="username" label="用户名" align="center" />
          <el-table-column prop="phone" label="手机号" align="center" />
          <el-table-column prop="email" label="邮箱" align="center" />
          <el-table-column prop="applyTime" label="申请时间" align="center" />
          <el-table-column label="当前状态" align="center">
            <font style="color: red">未审核</font>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-tooltip content="查看信息" placement="top">
                <el-button
                  size="mini"
                  icon="el-icon-search"
                  circle
                  @click="showDialog(scope.row)"
                />
              </el-tooltip>
              <el-tooltip content="审核账号信息" placement="top">
                <el-button
                  el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-check"
                  circle
                  @click="verifySupplierInfo(scope.row)"
                />
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          :current-page="paging.current"
          :page-sizes="paging.pageSizes"
          :page-size="paging.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paging.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <Dialog
        :visible="supplierUnreviewedVisible"
        :main-info="supplierMainInfo"
        :other-info="supplierOtherInfo"
        @cancel="cancel"
      />
    </el-card>
  </div>
</template>

<script>
// import { getToken } from "@/utils/auth";
import {
  getAllUnreviewedSupplierInfoByConditions,
  getSupplierOtherInfo,
  checkSupplierInfo,
} from "@/api/supplier.js";
import Dialog from "./components/Dialog";

export default {
  name: "SupplierUnreviewedManage",
  components: { Dialog },
  data() {
    return {
      searchForm: {
        queryConditions: "",
      },
      //organizationList: [],
      list: [],
      paging: {
        total: 0,
        current: 1,
        pageSizes: [5, 10, 15],
        size: 5,
      },
      supplierUnreviewedVisible: false,
      unreviewedSupplierLoading: false,
      supplierMainInfo: {},
      supplierOtherInfo: {},
      // editDialogVisible: false,
      dialogTitle: "",
      index: 0, // 0 添加；1 编辑；2 查看信息
      // headers: {
      //   Authorization: getToken(),
      // },
    };
  },
  computed: {
    exportUrl() {
      return (
        process.env.VUE_APP_BASE_API +
        "/manager/exportSupplierManInfoExcel/" +
        this.$store.getters.accountId
      );
    },
  },
  created() {
    this.unreviewedSupplierLoading = true;
    this.getData();
    // this.getOrganizations()
    // console.log(getToken())
  },
  methods: {
    // async getOrganizations() {
    //   const result = await getAllOrganizationInfos()
    //   this.organizationList = result.data
    //   // console.log('@@@@',this.organizationList)
    // },
    search() {
      this.paging.current = 1;
      this.unreviewedSupplierLoading = true;
      this.getData();
    },
    getData() {
      const form = {
        ...this.searchForm,
        page: this.paging.current,
        pageSize: this.paging.size,
      };
      getAllUnreviewedSupplierInfoByConditions(form)
        .then(({ data }) => {
          console.log(data);
          this.list = data.supplierInfos;
          //this.paging.size = data.size
          this.paging.total = data.totalCount;
          this.unreviewedSupplierLoading = false;
        })
        .catch((err) => {
          // 如果没有数据就显示为空
          this.list = [];
          // 返还每页显示的条数
          //this.paging.size = res.data.size;
          this.paging.total = 0;
          this.unreviewedSupplierLoading = false;
        });
    },
    handleSizeChange(val) {
      this.paging.current = 1;
      this.paging.size = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.paging.current = val;
      this.getData();
    },
    showDialog(row) {
      console.log(row);
      this.supplierMainInfo = row;
      getSupplierOtherInfo({ supplierId: row.id }).then((res) => {
        if (res.code === 200) {
          this.supplierOtherInfo = res.data;
          this.supplierUnreviewedVisible = true;
        }
      });
      this.index = 2;
    },
    verifySupplierInfo(row) {
      this.$confirm("审核供应商账号, 通过亦或驳回?", "提示", {
        distinguishCancelAndClose: true,
        confirmButtonText: "通过",
        cancelButtonText: "驳回",
        type: "warning",
      })
        .then(async () => {
          const verifyData = {
            managerId: this.$store.getters.accountId,
            state: 1,
            supplierId: row.id,
          };
          this.checkInfo(verifyData, "账号审核通过！");
        })
        .catch((action) => {
          const verifyData = {
            managerId: this.$store.getters.accountId,
            state: -1,
            supplierId: row.id,
          };
          console.log("@@action:", action);
          if (action === "close") {
            this.$message({
              type: "info",
              message: "取消当前操作",
            });
          } else {
            this.checkInfo(verifyData, "账号不予通过！");
          }
        });
    },
    checkInfo(verifyData, message) {
      checkSupplierInfo(verifyData)
        .then((result) => {
          console.log(result);
          if (result.code === 200) {
            this.$message({
              type: "success",
              message: message,
            });
            this.getData();
          }
        })
        .catch((err) => {
          this.$message({
            type: "error",
            message: err,
          });
        });
    },

    cancel() {
      if (this.index === 2) {
        this.supplierUnreviewedVisible = false;
      } else {
        this.editDialogVisible = false;
      }
      this.index = 0;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
.btn {
  float: right;
  margin-left: 20px;
}
</style>
